//
//  BMViewController.m
//  bum
//
//  Created by xingshuai on 2020/12/26.
//

#import "BMViewController.h"

#define WEAKSELF typeof(self) __weak weakSelf = self;
#define STRONGSELF typeof(weakSelf) __strong strongSelf = weakSelf;
#define kScreenWidth  [[UIScreen mainScreen] bounds].size.width
#define kScreenHeight [[UIScreen mainScreen] bounds].size.height

#import <Photos/Photos.h>
#import "BMCollectionViewCell.h"
static NSString *albumCollectionViewCell = @"ZBWAlbumCollectionViewCell";


@interface BMViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>///PHPhotoLibraryChangeObserver
/// 相册数组
@property (nonatomic, strong) NSMutableArray<BMAlbumModel *> *assetCollectionList;
/// 当前要显示的相册
@property (nonatomic, strong) BMAlbumModel *albumModel;
/// 相册列表
@property (nonatomic, strong) UICollectionView *albumCollectionView;

@end

@implementation BMViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"bum";
    //self.view.backgroundColor = UIColor.greenColor;
    
    [self.view addSubview:self.albumCollectionView];
    
//    [self checkAuthorizationStatus];
//    [self addKeyValueObserver];
//    [self registerChangeObserver];
//    [self loadAllPhotoData];
    
}

-(void)setAssetsList:(NSArray *)assetsList {
    _assetsList = assetsList;
    
    [self.albumCollectionView reloadData];
}

#pragma mark - <UI创建>
- (UICollectionView *)albumCollectionView {
    if (_albumCollectionView == nil) {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        flowLayout.minimumLineSpacing = 2.f;
        flowLayout.minimumInteritemSpacing = 2.f;
        
        _albumCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:flowLayout];
        
        _albumCollectionView.delegate = self;
        _albumCollectionView.dataSource = self;
        _albumCollectionView.backgroundColor = [UIColor whiteColor];
        _albumCollectionView.scrollEnabled = YES;
        _albumCollectionView.alwaysBounceVertical = YES;
        
        [_albumCollectionView registerClass:[BMCollectionViewCell class] forCellWithReuseIdentifier:albumCollectionViewCell];
    }
    
    return _albumCollectionView;
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat w = (kScreenWidth - 6.f) / 4.f;
    CGFloat h = (kScreenWidth - 6.f) / 4.f;
    return CGSizeMake(w, h);
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsMake(0, 0, 0, 0);
}

#pragma mark - <UICollectionViewDataSource>
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.assetsList.count; //self.albumModel.assets.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    BMCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:albumCollectionViewCell forIndexPath:indexPath];
    cell.contentView.backgroundColor = UIColor.orangeColor;
    if (self.assetsList.count > indexPath.row) {
        cell.cellAsset = [self.assetsList objectAtIndex:indexPath.row];
    }
    
    return cell;
}

-(NSMutableArray *)assetCollectionList {
    if (!_assetCollectionList) {
        _assetCollectionList = [[NSMutableArray alloc]init];
    }
    return _assetCollectionList;
}

- (void)dealloc
{
    //[self removeKeyValueObserver];
//    [self unregisterChangeObserver];
}



#pragma mark - Photo library change observer

//- (void)registerChangeObserver
//{
//    [[PHPhotoLibrary sharedPhotoLibrary] registerChangeObserver:self];
//}
//
//- (void)unregisterChangeObserver
//{
//    [[PHPhotoLibrary sharedPhotoLibrary] unregisterChangeObserver:self];
//}
//
//
///// PHPhotoLibrary监听: 选择图片之后的回调
//#pragma mark - PHPhotoLibraryChangeObserver
//- (void)photoLibraryDidChange:(PHChange *)changeInstance {
//    /// 在主线程刷新
//    // Call might come on any background queue. Re-dispatch to the main queue to handle it.
//    WEAKSELF
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [weakSelf.albumCollectionView reloadData];
//    });
//}



#pragma mark - Key-Value observer

//- (void)addKeyValueObserver
//{
//    [self addObserver:self
//           forKeyPath:@"selectedAssets"
//              options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
//              context:nil];
//}
//
//- (void)removeKeyValueObserver
//{
//    @try {
//        [self removeObserver:self forKeyPath:@"selectedAssets"];
//    }
//    @catch (NSException *exception) {
//        // do nothing
//    }
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
